package com.qdlc.p2p.biz.service.activity.usercard;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AdditionalUserCardService;
import com.qdlc.p2p.biz.service.ExperienceRuleService;
import com.qdlc.p2p.biz.service.activity.ActivityPlanEnum;
import com.qdlc.p2p.biz.service.activity.ActivityPlanException;
import com.qdlc.p2p.biz.service.activity.ActivityPlanHandlers;
import com.qdlc.p2p.biz.service.activity.ActivityPlanParam;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeEnum;
import com.qdlc.p2p.biz.service.activity.ActivityProgrammeParam;
import com.qdlc.p2p.common.core.ReturnResult;
import com.qdlc.p2p.common.enums.EnumCardType;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.common.util.LogFactory;
import com.qdlc.p2p.dal.dto.AdditionalUserCard;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.dto.User;

/**
 * 功能描述: "酒"、"掌"、"柜"卡片兑换
 * @author linya
 * @date 2016-5-9
 */
@Service
public class UserCardWineExchangeStrategy implements UserCardExchangeStrategy {

	private static final Logger log = LogFactory.COMMON_LOGGER;
	@Resource
	AdditionalUserCardService additionalUserCardService;
	
	@Resource
	ExperienceRuleService experienceRuleService;
	
	@Override
	public ReturnResult exchange(User user, long[] cardIds) {

		//必须是3张卡
		if(cardIds.length != 3){
			return new ReturnResult("请分别集齐酒、掌、柜三张卡进行兑换！");
		}
		
		//获取用户卡
		List<AdditionalUserCard> list = additionalUserCardService.findUserCards(user.getUserId(), cardIds);
		if(list == null || list.size() != 3){
			return new ReturnResult("卡片不正确,请核对后再兑换!");
		}
		
		Set<Integer> cardTypeSet = new HashSet<Integer>();
		for(AdditionalUserCard card: list){
			cardTypeSet.add(card.getType());
		}
		
		//判断卡片是否集齐
		boolean equals = CollectionUtils.isEqualCollection(cardTypeSet, EnumCardType.getWineCardSet());
		if(!equals){
			return new ReturnResult("没有集齐卡片,请核对后在兑换!");
		}
		
		//兑换体验券
		ReturnResult result = exchangeExperience(user);
		
		//使用卡片
		if(result.isSuccess()){
			additionalUserCardService.useUserCard(user.getUserId(), cardIds);
		}
		
		return result;
	}

	/**
	 * 兑换体验券
	 * @param user
	 * @return
	 */
	private ReturnResult exchangeExperience(User user){
		
		List<ExperienceRule> ruleList = experienceRuleService.findUsableExperienceRule();
		if(ruleList == null || ruleList.size() == 0){
			log.info("++++ 没有配置9999元体验券");
			return new ReturnResult("体验券已兑换完!");
		}
		
		boolean findExperience = false;
		ExperienceRule experienceRule = null;
		for(ExperienceRule rule: ruleList){
			if(rule.getAmount() == 9999){
				experienceRule = rule;
				findExperience = true;
				break;
			}
		}
		
		if(!findExperience){
			return new ReturnResult("体验券已兑换完!");
		}
		
		ActivityPlanParam planParam = new ActivityPlanParam();
		planParam.setActivityPlan(ActivityPlanEnum.USER_CARD);
		planParam.setUser(user);
		ActivityProgrammeParam activityParam = new ActivityProgrammeParam();
		activityParam.setAttatchmentObject(experienceRule);
		planParam.addProgrammeParam(ActivityProgrammeEnum.EXPERIENCE, activityParam);
		try {
			ActivityPlanHandlers.customHandle(planParam);
		} catch (ActivityPlanException e) {
			e.printStackTrace();
			log.info("++++ 酒卡兑换体验券失败!");
			return new ReturnResult("体验券已兑换完!");
		}
		
		return ReturnResult.SUCCESS;
	}
}
